package com.account_main.service;


import com.account_api.UserApi;
import com.account_main.mapper.UserMapper;
import com.account_main.prisist.repository.IUserRepository;

import com.account_main.prisist.repository.impl.UserRepository;
import com.account_protocol.pojo.dto.UserDTO;
import com.account_protocol.pojo.po.UserPO;
import com.account_protocol.pojo.vo.SelectByAccountVO;
import com.common.pojo.MyUserDetails;
import com.common.result.JsonResult;
import com.common.result.ResultEnum;
import com.common.utils.FileUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@Service
@Slf4j
@DubboService
public class UserService implements UserApi {

    @Autowired(required = false)
    private IUserRepository userRepository;
    @Autowired
    private UserMapper userMapper;
    @Value("${config.user_info_path}")
    private String USER_INFO_PATH;

    @Value("config.value")
    private String value;



//    public int addUser(UserDTO userDTO){
//        UserPO userPO=userDTO2PO(userDTO);
//        return userRepository.addUser(userPO);
//    }


    @Transactional
//            (rollbackFor = CreateUserDirectoryException.class)
    public JsonResult addUser(UserDTO userDTO){
        UserPO userPO= new UserPO();
        BeanUtils.copyProperties(userDTO,userPO);
        userPO.setIsUsing(1);
        userPO.setIsEnable(1);
        userPO.setIsAdmin(0);
        log.info("开始打印PO"+userPO);
        int result1=userMapper.addUser(userPO);
        long uid=userMapper.selectUidByAccount(userDTO.getAccount());
        int result2=createUserDictionary(uid);
        log.info("userService打印数据库操作结果:{}",result1);
        log.info("userService打印文件夹操作结果:{}",result2);


        File userInfoDirectory =new File(USER_INFO_PATH+"//user"+uid);
        userInfoDirectory.mkdir();
        if (!userInfoDirectory.exists()){
            String errString=null;
            errString.length();
        }
        if (result1 !=1|| result2!=1){
            String str=null;
            str.length();
        }
        if (result1==1 && result2==1){
            return  JsonResult.ok();
        }
        else {
            return JsonResult.ok();
        }



    }
    public JsonResult updateUserProfilePicture(MultipartFile multipartFile, SecurityContextHolder securityContextHolder){
        if (multipartFile==null){
            return new JsonResult(ResultEnum.FILE_NOT_ALLOWED_EMPTY);
        }
        Authentication authentication = securityContextHolder.getContext().getAuthentication();
        MyUserDetails myUserDetails =(MyUserDetails) authentication.getDetails();
        long uid = myUserDetails.getUid();

        FileUploadUtils fileUploadUtils = new FileUploadUtils();
        int resultCode = fileUploadUtils.uoloadPicture(multipartFile,uid,USER_INFO_PATH);
        if (resultCode==0){
            return JsonResult.ok();
        }else {
            return new JsonResult(ResultEnum.FILE_UPLOAD_FAILED);
        }
    }
    public SelectByAccountVO selectNicknameByAccount(String account){
//        SelectByAccountPO selectByAccountPO=new SelectByAccountPO();
//        selectByAccountPO.setAccount(account);
        SelectByAccountVO selectByAccountVO =userMapper.selectNicknameByAccount(account);
        log.info("我service看看你这个查询结果是个什么东西"+selectByAccountVO);
        return selectByAccountVO;

    }

    @Override
    public String sayHello() {
        return "hello";
    }

    private UserPO userDTO2PO(UserDTO userDTO){
        UserPO userPO=new UserPO();
        BeanUtils.copyProperties(userDTO,userPO);
        return userPO;
    }



    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    public int  createUserDictionary(long uid){
        File file = new File("D:\\baking" + uid);
        file.mkdirs();
        if (file.exists()){
            return 1;
        }else {
            return 0;
        }
    }
    private String test(){
        return "测试openFeign远程调用成功";
    }




}
